<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/utils.js"></script>
</head>

<body>
  <h1>注册</h1>
  用户名：<input type="text" id="nameInput">
  密码：<input type="password" id="pwdInput">
  年龄<input type="number" id="ageInput">
  <button>注册</button>
  <script>
    const nameInput = document.querySelector('#nameInput');
    const pwdInput = document.querySelector('#pwdInput');
    const ageInput = document.querySelector('#ageInput');
    document.querySelector('button').onclick = function () {
      var name = nameInput.value;
      var pwd = pwdInput.value;
      var age = ageInput.value;
      utils.post('../api/user/repeat-register.php', { name }, (resp) => {
        if (resp.code === 200) {
          alert('账户名已存在,请重新输入')
          nameInput.value = pwdInput.value = ageInput.value = '';
        } else if (resp.code === 201) {
          utils.post('../api/user/register.php', { name, pwd, age }, (resp) => {
            if (resp.code === 200) {
              alert(resp.body.msg);
              location.href = 'login.html';
            } else {
              alert(resp.body.msg);
              nameInput.value = pwdInput.value = ageInput.value = '';
            }
          })
        }
      })
    }
  </script>
</body>

</html>